// pages/test/test.js
const apiserverUrl = require('../../config').apiserverUrl;
const ExamQuestionsListUrl = require('../../config').ExamQuestionsListUrl;
const ExamSheetUrl = require('../../config').ExamSheetUrl;
const StartExamUrl = require('../../config').StartExamUrl;
const FinishExamUrl = require('../../config').FinishExamUrl;
const AnswerExamUrl = require('../../config').AnswerExamUrl;
const GetUserExamQuestionUrl = require('../../config').GetUserExamQuestionUrl;
const GetExamTimeLeftUrl = require('../../config').GetExamTimeLeftUrl;

var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    papersid: 0,
    userInfo: [],
    qlist: [],
    eqcount: 0,
    sheetList: [],
    currentIndex: 0,
    useranswer: "",
    totalcount: 0,
    totalpage: 0,
    page: 1,
    limit: 100,
    countdown: {
      times: 0,
      format: 'mm:ss'
    },
    preIndex: 0
  },
  onLoad: function (options) {
    this.setData({ userInfo: app.globalData.userInfo, papersid: options.papersid });
    //考试剩余时间
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: GetExamTimeLeftUrl, data: {
          papersid: this.data.papersid
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.setData({
            countdown:
            {
              times: res.data.Data,
              format: 'mm:ss'
            }
          });
        }
      }
    });
    //开始考试接口
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: StartExamUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid,
          modeltype: 1
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          //初始化题目列表
          this.getList();
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swiperchange: function (event) {
    if (event.detail.source == "touch") {
      var lastqindex = this.data.limit - 1;
      if (this.data.page == this.data.totalpage) {
        lastqindex = this.data.totalcount - (this.data.totalpage - 1) * this.data.limit - 1;
      }
      //console.log("lastqindex:" + lastqindex);
      //console.log("this.data.currentIndex:" + this.data.currentIndex);
      //console.log("event.detail.current:" + event.detail.current);
      if (this.data.currentIndex == 0 && event.detail.current == lastqindex) {
        if (this.data.page == 1)//首页不再向上翻页
        {
          wx.showModal({
            content: '已到最前',
            showCancel: false
          });
          this.setData({ currentIndex: 0, preIndex: 0 });
          return;
        }
        //上一页
        if (this.data.page > 1) {
          this.setData({
            loadbysheet: false,
            currentIndex: this.data.limit - 1,
            page: this.data.page - 1
          });
          this.getList();
        }
        this.setData({ preIndex: 0 });
      } else if (this.data.currentIndex == lastqindex && event.detail.current == 0) {
        if (this.data.page == this.data.totalpage)//末页不再向下翻页
        {
          wx.showModal({
            content: '已到最后',
            showCancel: false
          });
          this.setData({ currentIndex: lastqindex });
          return;
        }
        //下一页
        if ((this.data.page + 1) <= this.data.totalpage) {
          this.setData({
            loadbysheet: false,
            currentIndex: 0,
            page: this.data.page + 1
          });
          this.getList();
        }
        this.setData({ preIndex: 0 });
      }
      else {
        //console.log("在当前页面上");
        this.setData({
          currentIndex: event.detail.current,
          useranswer: this.data.qlist[event.detail.current].useranswer
        });
        //获取用户答案选项接口
        //this.getUserAnswer();
      }
      //防止swiper控件卡死
      if (this.data.currentIndex == 0 && this.data.preIndex > 1) {
        this.setData({ currentIndex: this.data.preIndex });
      }
      else {
        this.setData({ preIndex: this.data.currentIndex });
      }
    }
  },
  getList: function () {
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: ExamQuestionsListUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid,
          isexam: 1,
          page: this.data.page,
          limit: this.data.limit,
          loading: this.data.loading
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.setData({
            page: res.data.Data.page,
            totalcount: res.data.Data.totalcount,
            totalpage: res.data.Data.totalpage,
            qlist: res.data.Data.qlist,
            useranswer: res.data.Data.qlist[0].useranswer
          });
          if (this.data.loading) {
            this.setData({
              loading: false,
              currentIndex: res.data.Data.qindex,
              useranswer: this.data.qlist[res.data.Data.qindex].useranswer
            });
          }
          //this.getUserAnswer();
        }
      }
    });
  },
  showPopup() {
    //获取用户答题卡接口
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: ExamSheetUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid,
          modeltype: 1
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.setData({
            eqcount: res.data.Data.eqcount,
            sheetList: res.data.Data.examsheet
          });
        }
      }
    });

    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.show();
  },
  qnum_click: function (e) {
    var qindex = e.currentTarget.dataset.qindex;
    if (this.data.totalpage > 1) {
      var page = parseInt(qindex / this.data.limit);
      if (page == 0) {
        page = 1;
      } else {
        page = page + 1;
      }
      this.setData({ currentIndex: (qindex - (page - 1) * this.data.limit) });

      if (page != this.data.page) {
        this.setData({
          loadbysheet: true,
          page: page
        });
        this.getList();
      }
    } else {
      this.setData({
        currentIndex: qindex
      });
    }
    this.setData({
      useranswer: this.data.qlist[this.data.currentIndex].useranswer
    });
    //this.getUserAnswer();
  },
  getUserAnswer: function () {
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: GetUserExamQuestionUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid,
          modeltype: 1,
          questionid: this.data.qlist[this.data.currentIndex].id
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        this.setData({ useranswer: "" });
        if (res.data.IsSuccess && res.data.Data != null) {
          this.setData({ useranswer: res.data.Data.answer });
        } else {
          this.setData({ useranswer: "" });
        }
      }
    });
  },
  answer_click: function (e) {
    this.setData({ useranswer: e.currentTarget.dataset.sn });
    var err = this.data.qlist[this.data.currentIndex].answer == this.data.useranswer ? 1 : 2;
    //添加选择记录接口
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: AnswerExamUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid,
          modeltype: 1,
          questionindex: this.data.currentIndex,
          questionid: e.currentTarget.dataset.qid,
          answer: this.data.useranswer,
          err: err
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.data.qlist[this.data.currentIndex].useranswer = this.data.useranswer;
          this.setData({
            qlist: this.data.qlist
          });
        }
      }
    });
  },
  onEndCount: function () {
    //倒计时结束自动交卷
    var that = this;
    wx.showModal({
      title: '提示',
      content: "考试时间已到，将自动为您交卷!",
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            method: "post",//请求数据用get，添加数据post，修改数据put
            url: apiserverUrl,
            data: {
              method: "GET", url: FinishExamUrl, data: {
                papersid: that.data.papersid,
                userid: that.data.userInfo.userid
              }
            },
            header: {
              "Content-Type": "application/json"
            },
            success: res => {
              if (res.data.IsSuccess) {
                wx.redirectTo({
                  url: '../submit/submit?papersid=' + that.data.papersid,
                })
              }
            }
          });
        }
      }
    });
  },
  finishExam: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '还剩' + (that.data.qlist.length - that.data.eqcount) + '题未作答，确认交卷吗？',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          //交卷接口
          wx.request({
            method: "post",//请求数据用get，添加数据post，修改数据put
            url: apiserverUrl,
            data: {
              method: "GET", url: FinishExamUrl, data: {
                papersid: that.data.papersid,
                userid: that.data.userInfo.userid,
                modeltype: 1
              }
            },
            header: {
              "Content-Type": "application/json"
            },
            success: res => {
              if (res.data.IsSuccess) {
                wx.redirectTo({
                  url: '../submit/submit?papersid=' + that.data.papersid,
                })
              }
            }
          });
        } else {
          let popupComponent = that.selectComponent('.J_Popup');
          popupComponent && popupComponent.hide();
        }
      }
    });
  }
})